package com.ha.garbage;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.ha.garbage.adapter.GarbageListAdapter;
import com.ha.garbage.adapter.KindListAdapter;
import com.ha.garbage.model.Garbage;
import com.ha.garbage.model.GarbageKindVO;
import com.ha.garbage.model.Result;
import com.ha.garbage.service.garbageService;
import com.ha.garbage.utils.UserRetrofit;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;


public class GarbageFragment extends Fragment {
private RecyclerView recyclerView;
private GarbageListAdapter garbageListAdapter;
private List<Garbage> garbageList = new ArrayList<>();
private KindListAdapter kindListAdapter;
private List<GarbageKindVO> garbageKindVOList = new ArrayList<>();
private List<Garbage> garbageList1 = new ArrayList<>();
private EditText editText;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_garbage, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //初始化控件
        recyclerView = view.findViewById(R.id.recycleView);
        editText = view.findViewById(R.id.garbageName);

        //查询数据1
        Retrofit retrofit = UserRetrofit.retrofit();
        garbageService garbageService = retrofit.create(garbageService.class);

        Call<Result<List<Garbage>>> top10 = garbageService.getTop10();
        top10.enqueue(new Callback<Result<List<Garbage>>>() {
            @Override
            public void onResponse(Call<Result<List<Garbage>>> call, Response<Result<List<Garbage>>> response) {
                if(response.isSuccessful()){
                    Result<List<Garbage>> body = response.body();
                    if(body.getCode() == 1){
                        System.out.println("查询成功");
                        garbageList = body.getData();
                        garbageList1 = body.getData();
                        garbageListAdapter = new GarbageListAdapter(garbageList);
                        recyclerView.setAdapter(garbageListAdapter);
                    }else{
                        new AlertDialog.Builder(getContext())
                                .setMessage("查询不到该数据")
                                .setPositiveButton("确定",null)
                                .show();

                    }
                }
            }

            @Override
            public void onFailure(Call<Result<List<Garbage>>> call, Throwable t) {
                System.out.println("查询出错"+call.request().url().toString());
                System.out.println("错误原因"+t.toString());
            }
        });

        view.findViewById(R.id.btn_kind_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Call<Result<List<GarbageKindVO>>> kind = garbageService.getKind();
                kind.enqueue(new Callback<Result<List<GarbageKindVO>>>() {
                    @Override
                    public void onResponse(Call<Result<List<GarbageKindVO>>> call, Response<Result<List<GarbageKindVO>>> response) {
                        if(response.isSuccessful()){
                            Result<List<GarbageKindVO>> body = response.body();
                            if(body.getCode() == 1){
                                garbageKindVOList = body.getData();
                                //初始化adapter

                                kindListAdapter = new KindListAdapter(garbageKindVOList);
                                //重新放置
                                recyclerView.setAdapter(kindListAdapter);


                            }

                        }
                    }

                    @Override
                    public void onFailure(Call<Result<List<GarbageKindVO>>> call, Throwable t) {

                    }
                });
            }
        });

        //查询数据
       view.findViewById(R.id.btn_search).setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {

               if(editText.getText().toString().isEmpty()){
                   garbageListAdapter =   new GarbageListAdapter(garbageList1);
                   //设置适配器
                   recyclerView.setAdapter(garbageListAdapter);
                   return;
               }

               Call<Result<List<Garbage>>> call = garbageService.getGarbage(editText.getText().toString());
               call.enqueue(new Callback<Result<List<Garbage>>>() {
                   @Override
                   public void onResponse(Call<Result<List<Garbage>>> call, Response<Result<List<Garbage>>> response) {
                       if(response.isSuccessful()){
                           Result<List<Garbage>> body = response.body();
                           if(body.getCode() == 1){
                               System.out.println("查询成功");
                               List<Garbage> garbage = body.getData();
                               garbageList = garbage;
                               //初始化适配器
                               garbageListAdapter =   new GarbageListAdapter(garbageList);
                               //设置适配器
                               recyclerView.setAdapter(garbageListAdapter);

                           }else{
                               new AlertDialog.Builder(getContext())
                                       .setMessage("查询不到该数据")
                                       .setPositiveButton("确定",null)
                                       .show();

                           }
                       }
                   }

                   @Override
                   public void onFailure(Call<Result<List<Garbage>>> call, Throwable t) {

                       System.out.println("查询出错");
                   }
               });
           }
       });


    }
}